package practice;

import practice.EyeotaException.Status;

public class AssertUtils {
	public static void ensure(boolean condition) {
		ensure(condition, "Pre condition failure");
	}

	public static void ensure(boolean condition, String messageIfError) {
		if (!condition)
			throw new EyeotaException(messageIfError
					+ " (AssertUtils.ensure failed)",
					Status.ERROR_ASSERT_FAILED);
	}

	public static void ensureNotNull(Object obj, String messageIfError) {
		if (obj == null)
			throw new EyeotaException(messageIfError
					+ " (AssertUtils.ensureNotNull failed)",
					Status.ERROR_ASSERT_FAILED);
	}

	public static void ensureNotNullNotEmpty(String s, String messageIfError) {
		if ((s == null) || s.isEmpty())
			throw new EyeotaException(messageIfError
					+ " (AssertUtils.ensureNotNull failed)",
					Status.ERROR_ASSERT_FAILED);
	}

	public static void ensureNoNullElement(Iterable<? extends Object> objs) {
		if (objs == null)
			throw new EyeotaException(
					"objs itself is null (AssertUtils.ensureNoNullElement failed)", Status.ERROR_ASSERT_FAILED);					

		int index = 0;
		for (Object obj : objs) {
			if (obj == null)
				throw new EyeotaException(
						"Null element found at (zero based) index [" + index
								+ "] (AssertUtils.ensureNoNullElement failed)", Status.ERROR_ASSERT_FAILED);						
		}
	}

	public static void ensureNotNull(Object obj) {
		ensureNotNull(obj, "Found null object");
	}

	public static void ensureEqual(int expected, int actual,
			String messageIfError) {

		if (expected != actual) {
			String errMessage = "Expected [" + expected + "] actual [" + actual
					+ "] (AssertUtils.assertEquals failed)";

			if (messageIfError != null) {
				errMessage = messageIfError + ". " + errMessage;
			}

			throw new EyeotaException(errMessage, Status.ERROR_ASSERT_FAILED);					
		}
	}

	public static void ensureEqual(int expected, int actual) {

		ensureEqual(expected, actual, null);
	}
}